﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;

namespace SecurityModule
{
    public class EmailHelper
    {
        private readonly string _templatePath;
        private readonly string _sendingEmail;
        private readonly string _sendingName;
        private readonly string _linkRoot;

        public EmailHelper(string linkRoot, string sendingEmail, string sendingName)
        {
            _linkRoot = linkRoot;
            _sendingEmail = sendingEmail;
            _sendingName = sendingName;
        }

        public EmailHelper(string templatePath, string linkRoot, string sendingEmail, string sendingName)
        {
            _templatePath = templatePath;
            _linkRoot = linkRoot;
            _sendingEmail = sendingEmail;
            _sendingName = sendingName;
        }


        public string GetEmailBody()
        {
            string emailText;
            using (var reader = new StreamReader(_templatePath))
            {
                emailText = reader.ReadToEnd();
            }
            emailText = emailText.Replace("{root}", _linkRoot);
            emailText = emailText.Replace("{support_email}", _sendingEmail);
            emailText = emailText.Replace("{app_name}", _sendingName);
            return emailText;
        }

        public string GetEmailBody(Dictionary<string, string> fields)
        {
            var emailText = GetEmailBody();

            return fields.Aggregate(emailText, (current, field) => current.Replace(string.Format("{{{0}}}", field.Key), field.Value));
        }

        public string GetEmailBody(object obj, Dictionary<string, string> additionalFields = null)
        {
            var replacements = obj.GetType().GetProperties().
                ToDictionary(
                prop => prop.Name.ToLower(),
                prop =>
                {
                    var value = string.Empty;

                    if (prop.GetValue(obj) != null)
                        value = prop.GetValue(obj).ToString();
                    return value;
                }
            );
            if (additionalFields != null)
                foreach (var field in additionalFields)
                {
                    replacements.Add(field.Key, field.Value);
                }

            return GetEmailBody(replacements);
        }



        private string FormatSubject(string subject)
        {
            return string.Format("{0} - {1}", _sendingName, subject);
        }

        public void SendEmail(string recipientEmail, string subject, string body, string cc = null, bool htmlBody = true)
        {
            using (var mailMessage = new MailMessage())
            {
                mailMessage.To.Add(new MailAddress(recipientEmail));
                mailMessage.From = new MailAddress(_sendingEmail, _sendingName);
                mailMessage.Subject = FormatSubject(subject);
                mailMessage.Body = body;
                mailMessage.IsBodyHtml = htmlBody;

                if (!string.IsNullOrEmpty(cc))
                    mailMessage.CC.Add(cc);

                new SmtpClient().Send(mailMessage);
            }
        }

        public void SendEmailToSelf(string subject, string body, string cc = null, bool htmlBody = true)
        {
            SendEmail(_sendingEmail, subject, body, cc, htmlBody);
        }
    }
}
